﻿var app = angular.module("app", ["xeditable"]);
app.run(function (editableOptions) {
    editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
});

app.controller('EditableRowCtrl', function ($scope, $filter, $http) {
    $('#ajax_loader').show();
    
    $http.get('/api/locationapi/get').success(function (data) {
        $scope.locations = data;
        $('#ajax_loader').hide();
    })
    .error(function () {
        $('#ajax_loader').hide();
    });

    $scope.checkName = function (data, id) {

    };

    $scope.saveLocation = function (data, id) {
        //$scope.Location not updated yet
        if (id) {
            angular.extend(data, { id: id });
            return $http.post('/api/locationapi/update', data);
        } else {
            return $http.post('/api/locationapi/add', data).success(function (data) {
                $scope.locations[$scope.locations.length - 1].id = data.id;
            });
        }
    };

    // remove Location
    $scope.removeLocation = function (index) {
        var delLocation = $scope.locations[index];
        $('#myModal-' + index).modal('toggle');
        $('.modal-backdrop').remove();
        return $http.post('/api/locationapi/remove', delLocation).success(function () {
            $scope.locations.splice(index, 1);
        });
    };

    // add Location
    $scope.addLocation = function () {
        $scope.inserted = {
            id: '',
            name: ''
        };
        $scope.locations.push($scope.inserted);
    };
});